package com.yanqu.road.entity.activity.daomu.pvp;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@FieldTypeAnnotation(tableName = "t_c_activity_daomu_pvp_user_data")
public class DaoMuPvpUserData extends DataObject {

    @FieldTypeAnnotation(desc = "活动ID", select = true, update = true)
    private int activityId;

    @FieldTypeAnnotation(desc = "玩家ID", update = true)
    private long userId;

    @FieldTypeAnnotation(desc = "房间id")
    private int roomId;

    @FieldTypeAnnotation(desc = "位置时间")
    private DaoMuPvpPosition position = new DaoMuPvpPosition();

    @FieldTypeAnnotation(desc = "战斗属性")
    private DaoMuPvpProperty property = new DaoMuPvpProperty();

    @FieldTypeAnnotation(desc = "伙伴id")
    private int roleId;

    @FieldTypeAnnotation(desc = "报名状态")
    private boolean apply;

    @FieldTypeAnnotation(desc = "本轮资格")
    private boolean able;

    @FieldTypeAnnotation(desc = "是否淘汰")
    private boolean out;

    @FieldTypeAnnotation(desc = "淘汰类型")
    private int outType;

    @FieldTypeAnnotation(desc = "淘汰时间")
    private long outTime;

    @FieldTypeAnnotation(desc = "心跳时间")
    private long pingTime;

    @FieldTypeAnnotation(desc = "摸金等级")
    private int collectionLevel;

    @FieldTypeAnnotation(desc = "藏品分")
    private long collectionScore;

    @FieldTypeAnnotation(desc = "pve属性上传时间")
    private long pveUploadTime;

    @FieldTypeAnnotation(desc = "总积分")
    private long score;

    @FieldTypeAnnotation(desc = "轮次排名")
    private Map<Integer, Integer> roundRank = new HashMap<>();

    @FieldTypeAnnotation(desc = "轮次积分")
    private Map<Integer, Long> roundScore = new HashMap<>();

    /**
     * 子弹列表,不入库
     */
    private List<DaoMuPvpBulletData> bulletDataList = new ArrayList<>();

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            setOp(DataStatus.Update);
        }
    }

    public int getRoomId() {
        return roomId;
    }

    public void setRoomId(int roomId) {
        if (this.roomId != roomId) {
            this.roomId = roomId;
            setOp(DataStatus.Update);
        }
    }

    public DaoMuPvpPosition getPosition() {
        return position;
    }

    public void setPosition(DaoMuPvpPosition position) {
        if (position == null) {
            return;
        }
        this.position = position;
        setOp(DataStatus.Update);
    }

    public int getRoleId() {
        return roleId;
    }

    public void setRoleId(int roleId) {
        if (this.roleId != roleId) {
            this.roleId = roleId;
            setOp(DataStatus.Update);
        }
    }

    public boolean isApply() {
        return apply;
    }

    public void setApply(boolean apply) {
        if (this.apply != apply) {
            this.apply = apply;
            setOp(DataStatus.Update);
        }
    }

    public boolean isAble() {
        return able;
    }

    public void setAble(boolean able) {
        if (this.able != able) {
            this.able = able;
            setOp(DataStatus.Update);
        }
    }

    public boolean isOut() {
        return out;
    }

    public void setOut(boolean out) {
        if (this.out != out) {
            this.out = out;
            setOp(DataStatus.Update);
        }
    }

    public int getOutType() {
        return outType;
    }

    public void setOutType(int outType) {
        if (this.outType != outType) {
            this.outType = outType;
            setOp(DataStatus.Update);
        }
    }

    public long getOutTime() {
        return outTime;
    }

    public void setOutTime(long outTime) {
        if (this.outTime != outTime) {
            this.outTime = outTime;
            setOp(DataStatus.Update);
        }
    }

    public long getPingTime() {
        return pingTime;
    }

    public void setPingTime(long pingTime) {
        if (this.pingTime != pingTime) {
            this.pingTime = pingTime;
            setOp(DataStatus.Update);
        }
    }

    public DaoMuPvpProperty getProperty() {
        return property;
    }

    public void setProperty(DaoMuPvpProperty property) {
        if (property == null) {
            return;
        }
        this.property = property;
        setOp(DataStatus.Update);
    }

    public int getCollectionLevel() {
        return collectionLevel;
    }

    public void setCollectionLevel(int collectionLevel) {
        if (this.collectionLevel != collectionLevel) {
            this.collectionLevel = collectionLevel;
            setOp(DataStatus.Update);
        }
    }

    public long getCollectionScore() {
        return collectionScore;
    }

    public void setCollectionScore(long collectionScore) {
        if (this.collectionScore != collectionScore) {
            this.collectionScore = collectionScore;
            setOp(DataStatus.Update);
        }
    }

    public long getScore() {
        return score;
    }

    public void setScore(long score) {
        if (this.score != score) {
            this.score = score;
            setOp(DataStatus.Update);
        }
    }

    public Map<Integer, Integer> getRoundRank() {
        return roundRank;
    }

    public void setRoundRank(Map<Integer, Integer> roundRank) {
        if (roundRank == null) {
            return;
        }
        this.roundRank = roundRank;
        setOp(DataStatus.Update);
    }

    public Map<Integer, Long> getRoundScore() {
        return roundScore;
    }

    public void setRoundScore(Map<Integer, Long> roundScore) {
        if (roundScore == null) {
            return;
        }
        this.roundScore = roundScore;
        setOp(DataStatus.Update);
    }

    public long getPveUploadTime() {
        return pveUploadTime;
    }

    public void setPveUploadTime(long pveUploadTime) {
        if (this.pveUploadTime != pveUploadTime) {
            this.pveUploadTime = pveUploadTime;
            setOp(DataStatus.Update);
        }
    }

    /**
     * 存活状态
     */
    public boolean isLive() {
        if (!apply || !able || out) {
            return false;
        }
        if (property == null) {
            return false;
        }
        if (property.getCurHp() <= 0) {
            return false;
        }
        if (System.currentTimeMillis() < property.getReviveTime()) {
            return false;
        }
        return true;
    }

    /**
     * 无敌保护时间
     */
    public boolean isProtectTime() {
        return System.currentTimeMillis() <= property.getProtectTime();
    }

    public List<DaoMuPvpBulletData> getBulletDataList() {
        synchronized (bulletDataList) {
            return new ArrayList<>(bulletDataList);
        }
    }

    public void addBulletData(DaoMuPvpBulletData bulletData) {
        synchronized (bulletDataList) {
            bulletDataList.add(bulletData);
        }
    }

    public void destroyBulletData(String bulletId) {
        synchronized (bulletDataList) {
            bulletDataList.removeIf(bulletData -> bulletData.getBulletId().equals(bulletId));
        }
    }

    public void setBulletDataList(List<DaoMuPvpBulletData> list) {
        synchronized (bulletDataList) {
            bulletDataList = list;
        }
    }

    /**
     * 获取玩家所在的坐标
     */
    public Coordinate getCoordinate() {
        return new Coordinate(position.getX(), position.getY());
    }
}
